package com.ruoyi.wb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wb.domain.WbEntAuth;
import com.ruoyi.wb.domain.WbEnterpriseArchitecture;
import com.ruoyi.wb.domain.WbMaintainCompany;
import com.ruoyi.wb.dto.WbEnterpriseArchitectureDto;
import com.ruoyi.wb.enmus.RoleNameEnums;
import com.ruoyi.wb.mapper.WbEntAuthMapper;
import com.ruoyi.wb.mapper.WbEnterpriseArchitectureMapper;
import com.ruoyi.wb.service.IWbEntAuthService;
import com.ruoyi.wb.service.IWbEnterpriseArchitectureService;
import com.ruoyi.wb.service.IWbMaintainCompanyService;
import com.ruoyi.wb.vo.WbEnterpriseArchitectureVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 企业认证架构Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-24
 */
@Service
public class WbEnterpriseArchitectureServiceImpl implements IWbEnterpriseArchitectureService {
    @Autowired
    private WbEnterpriseArchitectureMapper wbEnterpriseArchitectureMapper;

    @Autowired
    private WbEntAuthMapper wbEntAuthMapper;

    @Autowired
    private IWbMaintainCompanyService iWbMaintainCompanyService;

    @Autowired
    private IWbEntAuthService iWbEntAuthService;

    /**
     * 查询企业认证架构
     *
     * @param id 企业认证架构主键
     * @return 企业认证架构
     */
    @Override
    public WbEnterpriseArchitecture selectWbEnterpriseArchitectureById(Long id) {
        return wbEnterpriseArchitectureMapper.selectWbEnterpriseArchitectureById(id);
    }

    /**
     * 查询企业认证架构列表
     *
     * @param wbEnterpriseArchitecture 企业认证架构
     * @return 企业认证架构
     */
    @Override
    public List<WbEnterpriseArchitecture> selectWbEnterpriseArchitectureList(WbEnterpriseArchitecture wbEnterpriseArchitecture) {
        //如果有维保公司则只能查看自己企业  否则是管理员可以查看所有企业
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = sysUser.getRoles();
       List<WbEnterpriseArchitecture> wbEnterpriseArchitectures = new ArrayList<>();


        for (SysRole role : roles) {
            if (role.getRoleName().equals(RoleNameEnums.ADMIN.getDescribe())||
                    role.getRoleName().equals(RoleNameEnums.FIRE_UNIT_MANAGER.getDescribe())||
                    role.getRoleName().equals(RoleNameEnums.FIREFIGHTER.getDescribe())

            ) {//超级管理员 消防单位
                startPage();
                wbEnterpriseArchitectures=wbEnterpriseArchitectureMapper.selectWbEnterpriseArchitectureList(wbEnterpriseArchitecture);
            } else if (role.getRoleName().equals(RoleNameEnums.MAINTAIN_ADMIN.getDescribe())) {//维保公司管理员
                List<WbMaintainCompany> wbMaintainCompanies = iWbMaintainCompanyService.selectWbMaintainCompanyByUserId(sysUser.getUserId());
                if (!CollectionUtils.isEmpty(wbMaintainCompanies)) {
                    wbEnterpriseArchitecture.setMaintainCompanyId(wbMaintainCompanies.get(0).getId());
                    startPage();
                    wbEnterpriseArchitectures=wbEnterpriseArchitectureMapper.selectWbEnterpriseArchitectureList(wbEnterpriseArchitecture);
                }
            }else if(role.getRoleName().equals(RoleNameEnums.DOMESTIC_CONSUMER.getDescribe())){//普通用户
                List<WbEntAuth> wbEntAuthList = iWbEntAuthService.selectWbEntAuthListByUserId(sysUser.getUserId());
                if (!CollectionUtils.isEmpty(wbEntAuthList)){
                    wbEnterpriseArchitecture.setEntAuthId(wbEntAuthList.get(0).getId());
                    startPage();
                    wbEnterpriseArchitectures=wbEnterpriseArchitectureMapper.selectWbEnterpriseArchitectureList(wbEnterpriseArchitecture);
                }

            }


        }
        return wbEnterpriseArchitectures;
    }

    /**
     * 新增架构
     *
     * @param wbEnterpriseArchitecture 企业认证架构
     * @return 结果
     */
    @Override
    public int insertWbEnterpriseArchitecture(WbEnterpriseArchitecture wbEnterpriseArchitecture) {
        SysUser sysUser=SecurityUtils.getLoginUser().getUser();
        List<WbMaintainCompany> wbMaintainCompanies = iWbMaintainCompanyService.selectWbMaintainCompanyByUserId(sysUser.getUserId());
        if (!CollectionUtils.isEmpty(wbMaintainCompanies)) {
            wbEnterpriseArchitecture.setMaintainCompanyId(wbMaintainCompanies.get(0).getId());
        }
        wbEnterpriseArchitecture.setCreateTime(DateUtils.getNowDate());
        return wbEnterpriseArchitectureMapper.insertWbEnterpriseArchitecture(wbEnterpriseArchitecture);
    }

    /**
     * 修改企业认证架构
     *
     * @param wbEnterpriseArchitecture 企业认证架构
     * @return 结果
     */
    @Override
    public int updateWbEnterpriseArchitecture(WbEnterpriseArchitecture wbEnterpriseArchitecture) {
        wbEnterpriseArchitecture.setUpdateTime(DateUtils.getNowDate());
        return wbEnterpriseArchitectureMapper.updateWbEnterpriseArchitecture(wbEnterpriseArchitecture);
    }

    /**
     * 批量删除企业认证架构
     *
     * @param ids 需要删除的企业认证架构主键
     * @return 结果
     */
    @Override
    public int deleteWbEnterpriseArchitectureByIds(Long[] ids) {
        return wbEnterpriseArchitectureMapper.deleteWbEnterpriseArchitectureByIds(ids);
    }

    /**
     * 删除企业认证架构信息
     *
     * @param id 企业认证架构主键
     * @return 结果
     */
    @Override
    public int deleteWbEnterpriseArchitectureById(Long id) {
        return wbEnterpriseArchitectureMapper.deleteWbEnterpriseArchitectureById(id);
    }


    /**
     * 根据企业id查询架构信息
     *
     * @param wbEnterpriseArchitectureDto 查询入参
     * @return 结果
     */

    @Override
    public AjaxResult selectWbEnterpriseArchitecturebyAuthId(WbEnterpriseArchitectureDto wbEnterpriseArchitectureDto) {
        WbEntAuth wbEntAuth = wbEntAuthMapper.selectWbEntAuthById(wbEnterpriseArchitectureDto.getEntAuthId());
        //查询企业认证信息
        if (ObjectUtils.isEmpty(wbEntAuth)) {
            return AjaxResult.error(0, "还未进行企业认证");
        }
        WbEnterpriseArchitectureVo wbEnterpriseArchitectureVo = BeanUtil.copyProperties(wbEntAuth, WbEnterpriseArchitectureVo.class);
        WbEnterpriseArchitecture wbEnterpriseArchitecture = new WbEnterpriseArchitecture();
        wbEnterpriseArchitecture.setEntAuthId(wbEntAuth.getId());
        List<WbEnterpriseArchitecture> wbEnterpriseArchitectures = wbEnterpriseArchitectureMapper.selectWbEnterpriseArchitectureList(wbEnterpriseArchitecture);
        wbEnterpriseArchitectureVo.setWbEnterpriseArchitectureList(wbEnterpriseArchitectures);
        return AjaxResult.success(wbEnterpriseArchitectureVo);
    }
}
